<?php

namespace bc\model\knowledge;

use bc\ar\MActiveRecord;

class KnowledgeModel extends MActiveRecord
{
	public $autoId = 'id';
	
        public static function tableName()
	{
		return '{{%knowledge}}';
	}
       
        public function getBackendList($params, $page = null, $pageSize = null)
	{
		$columns = self::getTableSchema()->name . '.*, sub_cate.cate_name as sub_cate_name ';
		$ar = $this->find ()->select($columns)
				->leftJoin('bc_knowledge_category AS sub_cate',  self::getTableSchema()->name . '.category_id = sub_cate.cate_id ')
				->where($params);
		if ($page && $pageSize)
		{
			$offset = $this->getOffset ( $page, $pageSize );
			$ar->offset($offset)->limit($pageSize);
		}
	
		if($this->sort == null)
			$ar->orderBy ( $this->autoId . ' DESC' );
		else
			$ar->orderBy ( $this->sort );
		
		return $ar->asArray()->all();
	}
        
        public function getKnowledgeList($params, $page = null, $pageSize = null)
    {    
               
                $this->columns = "bc_knowledge.id,bc_knowledge.title,bc_knowledge.content,bc_knowledge.image,bc_knowledge.link,bc_knowledge.start_time,bc_knowledge.end_time,bc_knowledge.view_count,bc_knowledge.ys_view_count,bc_knowledge_teacher.teacher_name";
                $ar = $this->find ()->select($this->columns)->leftJoin('bc_knowledge_teacher', 'bc_knowledge_teacher' . '.teacher_id = ' . self::getTableSchema()->name . '.teacher_id') ->where($params);
               
                //echo $ar->createCommand()->getRawSql();
                if ($page && $pageSize)
		{
			$offset = $this->getOffset ( $page, $pageSize );
			$ar->offset($offset)->limit($pageSize);
		}
	
		if($this->sort == null)
			$ar->orderBy ( $this->autoId . ' DESC' );
		else
			$ar->orderBy ( $this->sort );
		
		return $ar->asArray()->all();
    } 
}

